﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FireZone.DataStructure;
using FireZone.NativeWMDMInteface;
using MSWMDMLib;
using System.Runtime.InteropServices;
using System.Threading;

namespace FireZone
{
    internal static class MTPStorageItemExtensions
    {
        public static void SafeInvalidateStorageGlobal(this MTPStorageItem rootMTPStorageItem)
        {
            if (rootMTPStorageItem != null)
            {
                WMDMStorageGlobal wmdmStorageGlobal;
                rootMTPStorageItem.StorageItem.GetStorageGlobals(out wmdmStorageGlobal);
                uint nSizeLow, nSizeHigh;
                wmdmStorageGlobal.GetTotalSize(out nSizeLow, out nSizeHigh);
                rootMTPStorageItem.StorageTotalSpace = (ulong)nSizeLow + (((ulong)nSizeHigh) << 32);
                wmdmStorageGlobal.GetTotalFree(out nSizeLow, out nSizeHigh);
                rootMTPStorageItem.StorageFreeSpace = (ulong)nSizeLow + (((ulong)nSizeHigh) << 32);
            }
        }
        public static MTPStorageItem SafeGetChildFileStorageItem(this MTPStorageItem parentMTPStorageItem, string childFileStorageName)
        {
            if (parentMTPStorageItem == null) throw new ArgumentNullException("parentMTPStorageItem");
            var ret = parentMTPStorageItem.ChildStorages.FirstOrDefault((s) => { return string.Equals(s.StorageName, childFileStorageName, StringComparison.OrdinalIgnoreCase); });
            if (ret == default(MTPStorageItem))
            {
                WMDMStorage wmdmStg;
                parentMTPStorageItem.StorageItem.GetStorage(childFileStorageName, out wmdmStg);
                if (wmdmStg == null)
                {
                    ret = parentMTPStorageItem.MTPInsertSingleFileSync(null, CancellationToken.None, true, childFileStorageName);
                }
                else
                {
                    ret = new MTPStorageItem() { StorageItem = (IWMDMStorage3)wmdmStg, StorageName = childFileStorageName, };
                }
                parentMTPStorageItem.ChildStorages.AddOrUpdateAsync(ret);
            }
            return ret;
        }
        public static MTPStorageItem SafeGetChildFolderStorageItem(this MTPStorageItem parentMTPStorageItem, string childFolderStorageName)
        {
            if (parentMTPStorageItem == null) throw new ArgumentNullException("parentMTPStorageItem");
            var ret = parentMTPStorageItem.ChildStorages.FirstOrDefault((s) => { return string.Equals(s.StorageName, childFolderStorageName, StringComparison.OrdinalIgnoreCase); });
            if(ret == default(MTPStorageItem))
            {
                WMDMStorage wmdmStg = null;
                try
                {
                    parentMTPStorageItem.StorageItem.GetStorage(childFolderStorageName, out wmdmStg);
                }
                catch(COMException)
                {

                }                
                if(wmdmStg == null)
                {
                    IWMDMStorageControl3 stgCtrl = (IWMDMStorageControl3)parentMTPStorageItem.StorageItem;
                    stgCtrl.Insert3((uint)WMDM_MODE.WMDM_MODE_BLOCK | (int)WMDM_MODE.WMDM_CONTENT_FOLDER | (int)WMDM_MODE.WMDM_STORAGECONTROL_INSERTINTO, WMDM_STORAGE_ATTR.WMDM_FILE_ATTR_FOLDER, null, childFolderStorageName, null, null, null, null, out wmdmStg);                    
                }                
                ret = new MTPStorageItem() { StorageItem = (IWMDMStorage3)wmdmStg, StorageName = childFolderStorageName, };
                parentMTPStorageItem.ChildStorages.AddOrUpdateAsync(ret);
            }
            return ret;
        }
    }
}
